import 'package:flutter/material.dart';
import 'package:flutter_svg/flutter_svg.dart';
import 'package:moose_app/core/utils/navigate_util.dart';
import 'package:moose_app/ui/screens/address_choose/address_choose_screen.dart';
import 'package:moose_app/ui/values/values.dart';
import 'package:moose_app/ui/widgets/auth/auth_button.dart';

class ATHAddressAddBody extends StatefulWidget {
  @override
  _ATHAddressAddBodyState createState() => _ATHAddressAddBodyState();
}

enum ATHGender { male, female }

class _ATHAddressAddBodyState extends State<ATHAddressAddBody> {
  ATHGender _gender = ATHGender.male;

  @override
  Widget build(BuildContext context) {
    return SingleChildScrollView(
      child: Column(
        children: [
          _buildReceivingAddress(),
          _buildHouseNumber(),
          _buildContacts(),
          _buildGender(),
          _buildMobileNumber(),
          _buildLabel(),
          _buildSubmitButton()
        ],
      ),
    );
  }

  Widget _buildReceivingAddress() {
    return ATHAddAddressItemWrapper(
        left: "收货地址：",
        right: GestureDetector(
          behavior: HitTestBehavior.opaque,
          onTap: () {
            ATHNavigator.pushFromRight(
                context, ATHAddressChooseScreen.routeName);
          },
          child: Row(
            mainAxisAlignment: MainAxisAlignment.spaceBetween,
            children: [
              Row(
                children: [
                  SvgPicture.asset(
                    "assets/icons/icon_location.svg",
                    height: kSize28,
                    color: kColor66,
                  ),
                  Container(
                    margin: EdgeInsets.only(left: kSize8),
                    child: Text("点击选择",
                        style:
                            TextStyle(color: kColor66, fontSize: kFontSize30)),
                  )
                ],
              ),
              SvgPicture.asset(
                "assets/icons/icon_arrow_right.svg",
                height: kSize48,
              ),
            ],
          ),
        ));
  }

  Widget _buildHouseNumber() {
    return ATHAddAddressItemWrapper(
        left: "门牌号：",
        right: ATHAddressAddInput(hintText: "【详细地址，例 1 层 101 室】"));
  }

  Widget _buildContacts() {
    return ATHAddAddressItemWrapper(
      left: "联系人：",
      right: Container(
        alignment: Alignment.centerLeft,
        constraints: BoxConstraints(minHeight: kSize116),
        decoration:
            BoxDecoration(border: Border(bottom: BorderSide(color: kColorF1))),
        child: ATHAddressAddInput(hintText: "请填写收货人的姓名"),
      ),
      showDivider: false,
    );
  }

  Widget _buildMobileNumber() {
    return ATHAddAddressItemWrapper(
        left: "手机号：", right: ATHAddressAddInput(hintText: "请填写收货手机号码"));
  }

  Widget _buildGender() {
    return ATHAddAddressItemWrapper(
      left: "",
      right: Row(
        children: [
          Row(
            children: [
              Radio<ATHGender>(
                  value: ATHGender.male,
                  groupValue: _gender,
                  onChanged: (gender) {
                    this.setState(() {
                      _gender = gender;
                    });
                  }),
              Text("男")
            ],
          ),
          Row(
            children: [
              Radio<ATHGender>(
                  value: ATHGender.female,
                  groupValue: _gender,
                  onChanged: (gender) {
                    this.setState(() {
                      _gender = gender;
                    });
                  }),
              Text("女")
            ],
          )
        ],
      ),
    );
  }

  Widget _buildLabel() {
    return ATHAddAddressItemWrapper(
        left: "标签：",
        right: Row(
          children: [
            ATHAddressAddLabel(labelText: "家"),
            ATHAddressAddLabel(labelText: "公司"),
            ATHAddressAddLabel(labelText: "学校"),
          ],
        ));
  }

  Widget _buildSubmitButton() {
    return Container(
      child: ATHAuthButton(
        text: "保存地址",
      ),
    );
  }
}

class ATHAddressAddInput extends StatelessWidget {
  final String hintText;

  ATHAddressAddInput({this.hintText});

  @override
  Widget build(BuildContext context) {
    return Container(
      child: TextField(
        cursorColor: kColor66,
        decoration: InputDecoration(
          hintText: hintText,
          hintStyle: TextStyle(fontSize: kFontSize30),
          border: InputBorder.none,
        ),
      ),
    );
  }
}

class ATHAddressAddLabel extends StatelessWidget {
  final String labelText;

  ATHAddressAddLabel({this.labelText});

  @override
  Widget build(BuildContext context) {
    return Container(
      padding: EdgeInsets.all(kSize6),
      margin: EdgeInsets.only(right: kSize20),
      constraints: BoxConstraints(minWidth: kSize100),
      decoration: BoxDecoration(border: Border.all(color: kColorF1)),
      child: Text(
        labelText,
        textAlign: TextAlign.center,
      ),
    );
  }
}

class ATHAddAddressItemWrapper extends StatelessWidget {
  final String left;

  final Widget right;

  final bool showDivider;

  ATHAddAddressItemWrapper({this.left, this.right, this.showDivider = true});

  @override
  Widget build(BuildContext context) {
    return Container(
      height: kSize116,
      decoration: BoxDecoration(
          border: showDivider
              ? Border(bottom: BorderSide(color: kColorF1))
              : Border()),
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.center,
        mainAxisAlignment: MainAxisAlignment.center,
        children: [
          Row(
            mainAxisAlignment: MainAxisAlignment.spaceBetween,
            children: [
              Container(
                  constraints: BoxConstraints(minWidth: kSize180),
                  child: Text(
                    "$left",
                    style: TextStyle(
                        color: kColor33,
                        fontSize: kFontSize30,
                        fontWeight: FontWeight.w500),
                  )),
              Expanded(
                child: right,
              )
            ],
          ),
        ],
      ),
    );
  }
}
